package com.leyou.evaluate.service;

import com.leyou.common.utils.IdWorker;
import com.leyou.order.pojo.OrderEvaluate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MangoService {
    @Autowired
    private IdWorker idWorker;

    @Autowired
    private MongoTemplate mongoTemplate;

    //1.添加评论到mangoDB数据库
    public void saveEvaluate(OrderEvaluate orderEvaluate) {
        orderEvaluate.setId(idWorker.nextId());
        mongoTemplate.save(orderEvaluate);
    }

    //通过spuId查询所有订单
    public List<OrderEvaluate> findAll(Long spuId,Integer pitch) {

        switch (pitch){
          /*  case 0:
                Query query = new Query(Criteria.where("spuId").is(spuId));
                List<OrderEvaluate> orderEvaluates = mongoTemplate.find(query, OrderEvaluate.class);
                return orderEvaluates;*/
            case 1:
                Query query1 = new Query(Criteria.where("orderScore").is(1));
                List<OrderEvaluate> orderEvaluates1 = mongoTemplate.find(query1, OrderEvaluate.class);
                return orderEvaluates1;
            case 3:
                Query query3 = new Query(Criteria.where("orderScore").is(3));
                List<OrderEvaluate> orderEvaluates3 = mongoTemplate.find(query3, OrderEvaluate.class);
                return orderEvaluates3;
            case 5:
                Query query5 = new Query(Criteria.where("orderScore").is(5));
                List<OrderEvaluate> orderEvaluates5 = mongoTemplate.find(query5, OrderEvaluate.class);
                return orderEvaluates5;
            case 6:
                Query query6 = new Query(Criteria.where("orderImage").exists(true));
                List<OrderEvaluate> orderEvaluates6 = mongoTemplate.find(query6, OrderEvaluate.class);
                return orderEvaluates6;
            default:
                Query query = new Query(Criteria.where("spuId").is(spuId));
                List<OrderEvaluate> orderEvaluates = mongoTemplate.find(query, OrderEvaluate.class);
                return orderEvaluates;
        }

    }


    public void removeDemo(Long id) {
        OrderEvaluate mango = new OrderEvaluate();
        mango.setId(id);
        mongoTemplate.remove(mango);
    }

    public void updateDemo(OrderEvaluate orderEvaluate) {
        Query query = new Query(Criteria.where("id").is(orderEvaluate.getId()));
        Update update = new Update();
        mongoTemplate.updateFirst(query, update, OrderEvaluate.class);
    }

}